//
//  CZVisitor.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/2/25.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import SnapKit

/*
1.设置代理协议
2.设置代理方法
3.设置代理属性

4.遵循协议
5.设置代理对象
6.实现方法



*/



protocol CZVisitorDelegate: NSObjectProtocol {

//设置代理方法
    func didLogin ()
    
    func didRegister ()


}

class CZVisitor: UIView {

//设置代理属性
    
    weak var delegate: CZVisitorDelegate?
    
    
//1.重写构造函数
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.创建一个私有方法来设置视图和约束
    private func setupUI(){
    
    //添加控件
        addSubview(homeIconImageView)
        
        //添加遮罩视图
        
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        //转动动画居中对

        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //添加提示label -- 距离上边就是homeIconImageView
        // 距离中间
        // 宽的约束 216
        
        messageLable.snp_makeConstraints { (make) -> Void in
            
            //距离动画视图的底部+ 10的间距
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
        }
        
        //注册按钮
        /*
        距离左边是 -messageLabel的左边
        上边是 messageLabel的底部
        宽100 高30
        */

    registerButton.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(messageLable.snp_left)
        
        make.top.equalTo(messageLable.snp_bottom).offset(10)
        
        make.width.equalTo(100)
        
        make.height.equalTo(30)
       
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLable.snp_right)
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        
        //添加遮罩
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    //MARK:注册按钮
    
    
    //设置登陆方法
    
    func login () {
        
        delegate?.didLogin()
        print("登陆成功")
        
    }
    
    
    func register () {
        
        delegate?.didRegister()
        print("注册开始")
        
    }
    
    
    
    //添加动画
    func startAnimation(){
    
    //千万不要输错了
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_2_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5
        
        //切换视图的时候,保持动画不要被移除
        animation.removedOnCompletion = false
        
        //添加到
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
        
    
    }
    
    //对外提供一个方法
    func setInfo(message:String,image:String,animation:Bool){
    
        messageLable.text = message
        
        iconImageView.image = UIImage(named: image)
        
        
        if animation {
        
            homeIconImageView.hidden = false
            
            startAnimation()
        
        } else {
        
        homeIconImageView.hidden = true
        
          }
        
    }

    
    //3.懒加载设置首页动图
    
    private lazy var homeIconImageView: UIImageView = {
    
        let img = UIImageView()
        
        //img.image = UIImageView(named: "visitordiscover_feed_image_smallicon")
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        
    }()
    
    //通用的图片
    private lazy var iconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
    }()

    
    
    //提示语
    private lazy var messageLable : UILabel = {
    
        let label = UILabel()
        
        //设置属性
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜"
    
        return label
    
    }()
    
    //注册按钮
    
    private lazy var registerButton : UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
    
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        
        button.addTarget(self, action: "register", forControlEvents:UIControlEvents.TouchUpInside )
    
        
        return button
        
    
    }()
    //登陆
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登陆", forState: UIControlState.Normal)
        
        
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
    }()
    

    
//遮罩视图
    private lazy var maskImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
    
}
